/**
 * @(#)ContentActivity.java
 * 
 * @Version: 1
 * @JDK: jdk 1.6.0.XXX
 * @Module: MBA
 */
/*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2013-03-04     Yongkai.Yu    Created
 **********************************************
 */


package com.mba.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.mba.base.BaseActivity;
import com.mba.bean.Message;
import com.mba.bean.UserInfo;
import com.mba.service.MessageService;
import com.mba.util.AsyncImageLoader;
import com.mba.util.AsyncImageLoader.ImageCallback;
import com.mba.util.Tools;
import com.mba.widget.JListView;

/**
 *
 */
public class ContentActivity extends BaseActivity { 
    String weiboId = null;
    UserInfo user = null;
//    Tools tools = null;
    private Button contract;
    private TextView getName;
    private MessageService msgService;
	private Tools tools;
	private ArrayList<Message>  contentList;
	private Message info;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //无title     
        requestWindowFeature(Window.FEATURE_NO_TITLE);    
        
        setContentView(R.layout.content);
        contract = (Button) findViewById(R.id.content_activity_contract);
        contract.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(arg0.getContext(),ChartActivity.class);
				startActivity(intent);
			}
		} );
        Button contentBackBtn = (Button) findViewById(R.id.content_back_btn);
        contentBackBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
        // 
        Intent i = this.getIntent();
        if (!i.equals(null)) {
            Bundle b = i.getExtras();
            if (b != null) {
                    int p = b.getInt("position");
                    try {
                    	System.out.println(p);
                        init(p);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        
        loadButtomMenu();

    }
    

    /**
     * 初始化数据操作
     * 
     * @param weiboId
     * @throws JSONException
     */
    private void init( int position) throws JSONException {
    	
            
//            String text = "1994年出品的电影有：肖申克的救赎，阿甘正传，狮子王，真实的谎言，生死时速，变相怪杰，低俗小说，这个杀手不太冷，神探飞机头，阿呆与阿瓜，四个婚礼一个葬礼，燃情岁月，夜访吸血鬼，阳光灿烂的日子，活着，重庆森林，饮食男女，东邪西毒，精武英雄，赌神2，九品芝麻官，新少林五祖。";
            
            try {
            	
                String time = "2013-03-07 22:30";
                
                this.msgService = MessageService.getInstance();
                this.tools = Tools.getInstance();
                
                this.contentList = (ArrayList<Message>) this.msgService.getHomeTimeline(Tools.GET_LOCAL);
				this.info = contentList.get(position);
				
				String testTx = info.getText();
				String userName = info.getUser().getUserName();
				String userArea = info.getLocation();
				TextView utv = (TextView) findViewById(R.id.user_name);
	            utv.setText(userName);
	            TextView ttv = (TextView) findViewById(R.id.text);
	            ttv.setText(testTx);
	            TextView area = (TextView) findViewById(R.id.content_activity_area);
	            area.setText(userArea);
	            
			} catch (Exception e) {
				e.printStackTrace();
			}
            
            
            
            JListView contentActivityJlist = (JListView) findViewById(R.id.content_activity_report_list);
            contentActivityJlist.setAdapter(new BaseAdapter() {
				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					if(convertView==null){
						convertView= LayoutInflater.from(ContentActivity.this).inflate(R.layout.home_list_item_reports_layout, null);
					}
					final TextView date = (TextView) convertView.findViewById(R.id.home_list_item_report_date);
					TextView reports = (TextView) convertView.findViewById(R.id.home_list_item_report_text);
					ImageView avater = (ImageView) convertView.findViewById(R.id.report_avater);
					Button toChartWith = (Button) convertView.findViewById(R.id.replys_reply);
					final TextView userName = (TextView) convertView.findViewById(R.id.home_list_item_user_name);
					
					toChartWith.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
								getName = (TextView) findViewById(R.id.content_activity_get_name_tv);
								getName.setText("回复  "+userName.getText());
								getName.setTextSize(25.0f);
						}
					});
					
					avater.setOnClickListener( new OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent intent = new Intent(v.getContext(),UserMainPageActivity.class);
							Activity activity = (Activity) v.getContext();
							activity.startActivity(intent);
						}
					});
					
					AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
						//异步加载图片
					 Drawable avater_image = asyncImageLoader.loadDrawable(
						  info.getComments().get(position).getUser().getHeadPic(), avater,
			               new ImageCallback() {
			                   public void imageLoaded(Drawable drawable,
			                           ImageView iv, String url) {
			                       iv.setImageDrawable(drawable);
			                   }
			        });
					
					 if (avater_image != null) {
							avater.setImageDrawable(avater_image);	
					 }
						
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
					date.setText(sdf.format(info.getCreateAt()));
					userName.setText(info.getComments().get(position).getUser().getUserName());
					userName.setTextSize(12.0f);
					reports.setText(info.getComments().get(position).getText());
					return convertView;
				}
				
				@Override
				public long getItemId(int position) {
					return 0;
				}
				
				@Override
				public Object getItem(int position) {
					// TODO Auto-generated method stub
					return null;
				}
				
				@Override
				public int getCount() {
					// TODO Auto-generated method stub
					return 2;
				}
			});
            ImageView iv = (ImageView) findViewById(R.id.user_icon);
            
            String userIcon=this.info.getUser().getHeadPic();
            AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
			Drawable cachedImage = asyncImageLoader.loadDrawable(userIcon, iv,
                    new ImageCallback() {
                        public void imageLoaded(Drawable imageDrawable,
                                ImageView imageView, String imageUrl) {
                            imageView.setImageDrawable(imageDrawable);
                        }
                    });
			 
			if (cachedImage != null) {
				iv.setImageDrawable(cachedImage);	
			}

    }
    
    // 菜单条对象
    GridView gv_buttom_menu;
    // 保存关注和评论对象引用，方便更新操作
    HashMap<String, HashMap> gz_pl_map = new HashMap<String, HashMap>();
    /**
     * 加载菜单数据 
     */
    private void loadButtomMenu() {
    	/*
        gv_buttom_menu = (GridView) this.findViewById(R.id.gv_buttom_menu);
        gv_buttom_menu.setBackgroundResource(android.R.drawable.bottom_bar);
        gv_buttom_menu.setNumColumns(4);// 总共显示多少项
        gv_buttom_menu.setGravity(Gravity.CENTER);
        gv_buttom_menu.setVerticalSpacing(5);// 垂直间隔
        gv_buttom_menu.setHorizontalSpacing(5);// 水平间隔
       

        ArrayList data = new ArrayList();
        HashMap map = new HashMap();
        map.put("itemImage", R.drawable.pl);
        map.put("itemText", "刷新");
        data.add(map);
        map = new HashMap();
        map.put("itemImage", R.drawable.pl);
        map.put("itemText", "关注");
        gz_pl_map.put("gz", map);
        data.add(map);
        map = new HashMap();
        map.put("itemImage", R.drawable.pl);
        map.put("itemText", "收藏");
        data.add(map);
        map = new HashMap();
        map.put("itemImage", R.drawable.pl);
        map.put("itemText", "评论");
        gz_pl_map.put("pl", map);
        data.add(map);

        SimpleAdapter adapter = new SimpleAdapter(this, data,
                R.layout.item_menu, new String[] { "itemImage", "itemText" },
                new int[] { R.id.item_image, R.id.item_text });

        gv_buttom_menu.setAdapter(adapter);
        gv_buttom_menu.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                Intent intent = new Intent();
                switch (position) {
                case 0:
                    // intent.setClass(MainActivity.this, AddNewContact.class);
                    // startActivityForResult(intent, 0);
                    break;
                case 1:
                    //
                    // findViewById(R.id.ll_search).setVisibility(View.VISIBLE);
                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
                case 5:

                    break;
                }

            }
        });
        */
        
    }

    
}
